package com.fitbet.admin.web.controller.authority;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fitbet.admin.entity.principal.Principal;
import com.fitbet.admin.entity.search.principal.PrincipalSearch;
import com.fitbet.admin.entity.security.Login;
import com.fitbet.admin.service.principal.PrincipalService;
import com.fitbet.admin.web.resolver.Secure;
/**
 * 管理员管理
 * @author sjh
 *
 */
@Controller
@RequestMapping("/principals")
@MenuSetting("menu-authority")
public class PrincipalController {

	public static final Logger logger = LoggerFactory.getLogger(PrincipalController.class);
	
	@Autowired
	private PrincipalService principalService;
	
	/**
	 * 
	 * @param login
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@MenuSetting("submenu-principal-management")
	public ModelAndView list(@Secure Login login, Page page) {
		
		ModelAndView modelAndView = new ModelAndView("authority/principal/principal-list");
		
		int totals = principalService.count(login);
		
        if (totals > 0) {
            
            List<Principal> principals = principalService.list(login, page.with(totals));
            
            modelAndView.addObject("principals", principals);
            modelAndView.addObject("page", page);

        }
		
		return modelAndView;
		
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public ModelAndView search() {
		
		ModelAndView modelAndView = new ModelAndView("authority/principal/principal-search");
		
		return modelAndView;
		
	}
	
	/**
	 * 
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
	@MenuSetting("submenu-principal-management")
	public ModelAndView onSearch(PrincipalSearch search, Page page, HttpServletRequest request) {
		
		ModelAndView modelAndView = new ModelAndView("authority/principal/principal-list");
		
		int totals = principalService.countSearch(Principal.class, search.getPreparedFilter(false));
        if (totals > 0) {

            List<Principal> principals = principalService.search(Principal.class, search, page.with(totals));
            modelAndView.addObject("principals", principals);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));

        }
		
		return modelAndView;
		
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value ="/create", method = RequestMethod.GET)
	@MenuSetting("submenu-principal-management")
	public ModelAndView create() {
		
		ModelAndView modelAndView = new ModelAndView("authority/principal/principal-create");
		
		return modelAndView;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public HttpResult onCreate(Principal principal) {
		
		HttpResult httpResult = new HttpResult();
		
		try {

                principalService.createPrincipal(principal);
                httpResult.success().message("新增用户成功");

        } catch (BusinessException e) {
            
            httpResult.fail(e.getCode()).message(e.getMessage());
            logger.error("onCreate occurs error: ", e);

        } catch (Exception e) {
            
            logger.error("onCreate occurs error: ", e);
            httpResult.fail("500").message("新增用户失败");
            
        }
		
		return httpResult;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	@MenuSetting("submenu-principal-management")
	public ModelAndView edit(@PathVariable("id") Long id) {
		
		ModelAndView modelAndView = new ModelAndView("authority/principal/principal-edit");
		
		Principal principal = principalService.load(Principal.class, id);
		modelAndView.addObject("principal", principal);
		
		return modelAndView;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value ="/{id}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public HttpResult onEdit(@PathVariable Long id, Principal principal) {
		
		HttpResult httpResult = new HttpResult();
		
		try {
            principalService.edit(id, principal);
            httpResult.success().message("修改成功");
        } catch (BusinessException e) {
            logger.error("onEdit occurs error: ", e);
            httpResult.fail(e.getCode()).message(e.getMessage());
        } catch (Exception e) {
            logger.error("#onEdit() - principal team fail, exception is [{}]", e);
            httpResult.fail("500").message("修改失败");
        }
		
		return httpResult;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/freeze", method = RequestMethod.GET)
	@MenuSetting("submenu-principal-management")
	public ModelAndView freeze(@PathVariable("id") Long id) {
		
		ModelAndView modelAndView = new ModelAndView("authority/principal/principal-freeze");
		
		modelAndView.addObject("id", id);
		
		return modelAndView;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/freeze", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public HttpResult onFreeze(@PathVariable("id") Long id) {
		
		HttpResult httpResult = new HttpResult();
		
		try {

            Principal principal = principalService.load(Principal.class, id);

            principalService.freeze(principal);

            httpResult.success().message("冻结成功！");

        } catch (Exception e) {

            httpResult.fail("500").message("冻结失败！");
        }

		
		return httpResult;
		
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/unfreeze", method = RequestMethod.GET)
	@MenuSetting("submenu-principal-management")
	public ModelAndView unfreeze(@PathVariable("id") Long id) {
		
		ModelAndView modelAndView = new ModelAndView("authority/principal/principal-unfreeze");
		
		modelAndView.addObject("id", id);
		
		return modelAndView;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/unfreeze" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public HttpResult onUnfreeze(@PathVariable("id") Long id) {
		
		HttpResult httpResult = new HttpResult();
		
		 try {

	            Principal principal = principalService.load(Principal.class, id);

	            principalService.activate(principal);

	            httpResult.success().message("激活成功！");

	        } catch (Exception e) {

	            httpResult.fail("500").message("激活失败！");
	        }
		
		return httpResult;
		
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/reset-password", method = RequestMethod.GET)
	@MenuSetting("submenu-principal-management")
	public ModelAndView resetPassword(@PathVariable("id") Long id) {
		
		ModelAndView modelAndView = new ModelAndView("authority/principal/principal-reset-password");
		
		modelAndView.addObject("id", id);
		
		return modelAndView;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/reset-password" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public HttpResult onResetPassword(@PathVariable("id") Long id) {
		
		HttpResult httpResult = new HttpResult();
		
		try {

            Principal principal = principalService.load(Principal.class, id);

            principalService.resetPassword(principal);

            httpResult.success().message("重置密码成功！");

        } catch (Exception e) {

            httpResult.fail("500").message("重置密码失败！");
        }
		
		return httpResult;
		
	}
	
}
